package com.hz.springbootschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.springbootschedule.dao.UserMapper;
import com.hz.springbootschedule.pojo.SmbmsUser;
import com.hz.springbootschedule.service.UserService;
import com.hz.springbootschedule.utils.RedisUtil;
import com.hz.springbootschedule.utils.SHA256Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author shkstart
 * @create 2023-02-13 16:23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,SmbmsUser> implements UserService {


    @Autowired
    private RedisUtil redisUtil;
//    @Autowired
//    private RedisTemplate redisTemplate;

    public SmbmsUser login(String username,String pwd) {

        SmbmsUser smbmsUser = this.getOne(new QueryWrapper<SmbmsUser>().select("id,username,userCode")
                .eq("userName", username).eq("userPassword", SHA256Util.makeHash(pwd,"wzy")));

        if(smbmsUser!=null){
            //令牌   使用JWT生成token
            String token = UUID.randomUUID().toString().replace("-","");

            redisUtil.setObj("login:"+token,smbmsUser);
            smbmsUser.setToken(token);
        }

        return smbmsUser;
    }

}
